//
//  CServerSettingMgr.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/20.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CServerSettingMgr.h"
#import "CServerSettingModel.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"
@implementation CServerSettingMgr

//获得服务器配置
-(CServerSettingModel *)getSetting
{
    __block CServerSettingModel *serverSetting = nil;
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] commonDatabasePath]];
    [[FMShareDataBase shareDataBase].commonShareQueue inDatabase:^(FMDatabase *db) {
        
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM SERVER"];
        
        FMResultSet *result =  [db  executeQuery:sql];
        if(result != nil)
        {
            while ([result next]) {
                serverSetting = [[CServerSettingModel alloc] init];
                serverSetting.IMIp = [result stringForColumn:@"IMIp"];
                serverSetting.IMPort = [result intForColumn:@"IMPort"];
                serverSetting.OpenFireIP = [result stringForColumn:@"OpenFireIP"];
                serverSetting.OpenFirePort = [result intForColumn:@"OpenFirePort"];
                serverSetting.companyId = [result stringForColumn:@"companyId"];
                serverSetting.companyStatus = [result stringForColumn:@"companyStatus"];
                serverSetting.companyName = [result stringForColumn:@"companyName"];
                serverSetting.companyTelePhone = [result stringForColumn:@"companyTelePhone"];
                
                serverSetting.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
                serverSetting.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
                serverSetting.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            }
        }
        [result close];
    }];
    return serverSetting;
}


-(BOOL)insetServerSetting:(CServerSettingModel *)serverSetting
{
    if(serverSetting == nil || M_ISSTRNULL(serverSetting.companyId))
    {
        return NO;
    }

//    NSLog(@"%@",[[CFolderMgr shareInstance] commonDatabasePath]);
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].commonShareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdateWithFormat:@"INSERT INTO SERVER(IMIp, IMPort, OpenFireIP, OpenFirePort,companyId,companyName,companyStatus,companyTelePhone,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (%@, %@, %@, %@,%@,%@,%@,%@,%@,%@,%@)", M_STOREVALUE(serverSetting.IMIp),@(serverSetting.IMPort),M_STOREVALUE(serverSetting.OpenFireIP),@(serverSetting.OpenFirePort), M_STOREVALUE(serverSetting.companyId),M_STOREVALUE(serverSetting.companyName),M_STOREVALUE(serverSetting.companyStatus),M_STOREVALUE(serverSetting.companyTelePhone),M_STOREVALUE(serverSetting.yuLiuZiduan1),@(serverSetting.yuLiuZiduan2),M_STOREVALUE(serverSetting.yuLiuZiduan3)];
        }];
    

    return ret;
}
-(BOOL)deleServerSetting
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] commonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].commonShareQueue inDatabase:^(FMDatabase *db) {
    ret = [db executeUpdate:@"DELETE from SERVER"];
        
    }];
    return ret;
}

//更新服务器配置
-(BOOL)updateServerSetting:(CServerSettingModel *)lastSetting
{
    
    __block BOOL ret = NO;
    if(lastSetting == nil)
    {
        return ret;
    }
 
    __block BOOL isExist = NO;
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] commonDatabasePath]];
    [[FMShareDataBase shareDataBase].commonShareQueue inDatabase:^(FMDatabase *db) {
        
        NSInteger index = 0;
        NSString *sql = [NSString stringWithFormat:@"SELECT identifer FROM SERVER"];
        FMResultSet *result =  [db  executeQuery:sql];
        if(result != nil)
        {
            while ([result next]) {
                index = [result intForColumn:@"identifer"];
                if(index > 0)
                {
                    isExist = YES;
                }
            }
        }
        if(isExist)
        {
            ret = [db executeUpdateWithFormat:@"UPDATE SERVER SET IMIp = %@,  IMPort = %zd,  OpenFireIP = %@, OpenFirePort = %d, companyId = %@ where identifer = %zd", lastSetting.IMIp?lastSetting.IMIp:@"", lastSetting.IMPort,!M_ISSTRNULL(lastSetting.OpenFireIP)?lastSetting.OpenFireIP:@"", lastSetting.OpenFirePort, index];
        }
        else
        {
            ret = [db executeUpdateWithFormat:@"INSERT INTO SERVER(IMIp, IMPort, OpenFireIP, OpenFirePort,companyId,companyName,companyStatus,companyTelePhone,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (%@, %@, %@, %@,%@,%@,%@,%@,%@,%@,%@)", M_STOREVALUE(lastSetting.IMIp),@(lastSetting.IMPort),M_STOREVALUE(lastSetting.OpenFireIP),@(lastSetting.OpenFirePort), M_STOREVALUE(lastSetting.companyId),M_STOREVALUE(lastSetting.companyName),M_STOREVALUE(lastSetting.companyStatus),M_STOREVALUE(lastSetting.companyTelePhone),M_STOREVALUE(lastSetting.yuLiuZiduan1),@(lastSetting.yuLiuZiduan2),M_STOREVALUE(lastSetting.yuLiuZiduan3)];
        }
        [result close];
    }];
    return ret;
}


@end
